package com.draven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.BusinessException;
import com.draven.entity.Bookmark;
import com.draven.mapper.BookmarkMapper;
import com.draven.service.BookmarkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 网络地址服务实现类
 */
@Service
public class BookmarkServiceImpl extends ServiceImpl<BookmarkMapper, Bookmark> implements BookmarkService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(Long userId, String title, String url, String category, Integer sort, Integer isFavorite) {
        // 创建网络地址
        Bookmark bookmark = new Bookmark();
        bookmark.setUserId(userId);
        bookmark.setTitle(title);
        bookmark.setUrl(url);
        bookmark.setCategory(category);
        bookmark.setSort(sort == null ? 0 : sort);
        bookmark.setIsFavorite(isFavorite == null ? 0 : isFavorite);

        save(bookmark);
        return bookmark.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Long id, Long userId, String title, String url, String category, Integer sort, Integer isFavorite) {
        // 查询网络地址
        Bookmark bookmark = getDetail(id, userId);

        // 更新信息
        bookmark.setTitle(title);
        bookmark.setUrl(url);
        bookmark.setCategory(category);
        if (sort != null) {
            bookmark.setSort(sort);
        }
        if (isFavorite != null) {
            bookmark.setIsFavorite(isFavorite);
        }

        updateById(bookmark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id, Long userId) {
        // 查询网络地址
        Bookmark bookmark = getDetail(id, userId);

        // 删除网络地址
        removeById(id);
    }

    @Override
    public Bookmark getDetail(Long id, Long userId) {
        // 查询网络地址
        Bookmark bookmark = getById(id);
        if (bookmark == null) {
            throw new BusinessException(400, "网络地址不存在");
        }

        // 校验所有权
        if (!bookmark.getUserId().equals(userId)) {
            throw new BusinessException(403, "无权限操作");
        }

        return bookmark;
    }

    @Override
    public Page<Bookmark> page(Long userId, String title, String url, String category, Integer isFavorite, Integer pageNum, Integer pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<Bookmark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bookmark::getUserId, userId);
        wrapper.like(StringUtils.isNotBlank(title), Bookmark::getTitle, title);
        wrapper.like(StringUtils.isNotBlank(url), Bookmark::getUrl, url);
        wrapper.eq(StringUtils.isNotBlank(category), Bookmark::getCategory, category);
        wrapper.eq(isFavorite != null, Bookmark::getIsFavorite, isFavorite);
        wrapper.orderByAsc(Bookmark::getSort);
        wrapper.orderByDesc(Bookmark::getCreateTime);

        // 分页查询
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer toggleFavorite(Long id, Long userId) {
        // 查询网络地址
        Bookmark bookmark = getDetail(id, userId);

        // 切换收藏状态
        Integer isFavorite = bookmark.getIsFavorite() == 0 ? 1 : 0;
        bookmark.setIsFavorite(isFavorite);

        updateById(bookmark);
        return isFavorite;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSort(Long id, Long userId, Integer sort) {
        // 查询网络地址
        Bookmark bookmark = getDetail(id, userId);

        // 更新排序
        bookmark.setSort(sort);

        updateById(bookmark);
    }
    
    @Override
    public int countByUserId(Long userId) {
        LambdaQueryWrapper<Bookmark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bookmark::getUserId, userId);
        return (int) count(wrapper);
    }
    
    @Override
    public int countFavoriteByUserId(Long userId) {
        LambdaQueryWrapper<Bookmark> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bookmark::getUserId, userId);
        wrapper.eq(Bookmark::getIsFavorite, 1);
        return (int) count(wrapper);
    }
} 